#ifndef CUSTOM
#define CUSTOM
#include <qgraphicsscene.h>
#include<qgraphicsitem.h>
#include<qgraphicsview.h>
#include<qevent.h>
#include<qgraphicssceneevent.h>
#include <qgraphicstransform.h>
#include<qtransform.h>
#include<math.h>
#include<qpainterpath.h>
class TopLeftPoint:public QGraphicsEllipseItem
{
public:
	QGraphicsItem* controledItem;
	TopLeftPoint(qreal x,qreal y,QGraphicsItem* item);
	QGraphicsItem* getControlledItem();
	void mousePressEvent(QGraphicsSceneMouseEvent* event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
};
class TopRightPoint:public QGraphicsEllipseItem
{
public:
	QGraphicsItem* controledItem;
	TopRightPoint(qreal x,qreal y,QGraphicsItem* item);
	QGraphicsItem* getControlledItem();
	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
};
class BottomLeftPoint:public QGraphicsEllipseItem
{
public:
	QGraphicsItem* controledItem;
	BottomLeftPoint(qreal x,qreal y,QGraphicsItem* item);
	QGraphicsItem* getControlledItem();
	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
};
class BottomRightPoint:public QGraphicsEllipseItem
{
public:
	QGraphicsItem* controledItem;
	BottomRightPoint(qreal x,qreal y,QGraphicsItem* item);
	QGraphicsItem* getControlledItem();
	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
};
class RotationCenterPoint:public QGraphicsEllipseItem
{
public:
	QGraphicsItem* controledItem;
	RotationCenterPoint(qreal x,qreal y,QGraphicsItem* item);
	QGraphicsItem* getControlledItem();
	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
};
class RotationControlPoint:public QGraphicsEllipseItem
{
public:
	QGraphicsItem* controledItem;
	RotationControlPoint(qreal x,qreal y,QGraphicsItem* item);
	QGraphicsItem* getControlledItem();
	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
};
class TopCenterPoint:public QGraphicsEllipseItem
{
public:
	QGraphicsItem* controledItem;
	TopCenterPoint(qreal x,qreal y,QGraphicsItem* item);
	QGraphicsItem* getControlledItem();
	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
};
class RightCenterPoint:public QGraphicsEllipseItem
{
public:
	QGraphicsItem* controledItem;
	RightCenterPoint(qreal x,qreal y,QGraphicsItem* item);
	QGraphicsItem* getControlledItem();
	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
};
enum ItemType{
	RectItem,
	EllipseItem,
	LineItem,
	PathItem
};
class customRectItem:public QGraphicsPathItem
{
	
public:
	bool edited;
	ItemType itemtype;
	QPainterPath painterpath;
	QTransform translationTr;
	QTransform skewTr;
	QTransform rotationTr;
	QTransform scaleTr;
	QTransform axisTr;
	qreal currentWidth;
	qreal currentHeight;
	qreal oriWidth;
	qreal oriHeight;
	qreal xscale;
	qreal yscale;
	QPointF tlp; 
	QPointF trp;
	QPointF blp;
	QPointF brp;
	QPointF rcp;
	QPointF rconp;
	QPointF tcp;
	QPointF rcenterp;
	QGraphicsScene* scene;
	TopLeftPoint* topleftpoint;
	TopRightPoint* toprightpoint;
	BottomLeftPoint* bottomleftpoint;
	BottomRightPoint* bottomrightpoint;
	RotationCenterPoint* rotationcenterpoint;
	RotationControlPoint* rotationcontrolpoint;
	TopCenterPoint* topcenterpoint;
	RightCenterPoint* rightcenterpoint;
	customRectItem(ItemType itemtype,qreal x,qreal y,qreal w,qreal h,QGraphicsScene* s);
	void updatePoints();
	void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
	QVariant itemChange (GraphicsItemChange change,const QVariant& value);
	void mousePressEvent(QGraphicsSceneMouseEvent* event);
	QPointF getAxis(){return this->mapToScene(this->rcp);};
	ItemType getItemType(){return this->itemtype;}
	void refresh()
	{
		QPointF axis=this->getAxis();
		QTransform translation=QTransform(1,0,0,1,axis.x(),axis.y());
		QPointF realcenter=rcp;
		QTransform tr0=QTransform(1,0,0,1,-realcenter.x(),-realcenter.y());
		resetTransform();
		setPos(0,0);
		setTransform(translation);
		setTransform(this->skewTr,true);
		setTransform(rotationTr,true);
		setTransform(scaleTr,true);
		setTransform(tr0,true);
		updatePoints();
	}
	qreal getXscale()
	{
		return this->scaleTr.m11();
	}
	qreal getYscale()
	{
		return this->scaleTr.m22();
	}
	void SetPosition(qreal x,qreal y)
	{
		this->setPos(0,0);
		this->resetTransform();
		QTransform tr=QTransform(1,0,0,1,x,y);
		this->setTransform(tr);
		this->setTransform(this->skewTr,true);
		this->setTransform(this->rotationTr,true);
		this->setTransform(this->scaleTr,true);
		QTransform tr0=QTransform(1,0,0,1,-(this->rcp.x()),-(this->rcp.y()));
		this->setTransform(tr0,true);
		this->updatePoints();
	}
	void SetScale(qreal x,qreal y)
	{
		QTransform trs=QTransform(x,0,0,y,0,0);
		this->scaleTr=trs;
		this->refresh();

	}
	void SetRotation()
	{
		QTransform rtr=QTransform();
	}
	void hidePoints();
	void showPoints();
};


#endif